
<template>
  <div class="home-page">
    <!-- tab标签栏    animated加上解决tab切换保存当前阅读位置高度的问题-->
    <van-tabs animated v-model="activeIndex">
      <van-tab v-for="item in MyChannels" :key="item.id" :title="item.name">
        <article-list :channelId="item.id"></article-list>
      </van-tab>
    </van-tabs>
    <!-- <geek-icon name="weixin"></geek-icon> -->
    <!-- 右侧搜索和内容频道 -->
    <!-- 按钮 -->
    <div class="btn-wrapper">
      <geek-icon name="search"></geek-icon>
      <geek-icon name="channel" @click.native="openChannel = true"></geek-icon>
    </div>
    <!-- 频道组件 -->
    <!-- <article-channel :value="openChannel" @input="openChannel = $event"></article-channel> -->
    <!--  :value="openChannel" @input="openChannel = $event" 简写成v-model -->
    <!-- :activeIndex="activeIndex"
      @update:activeIndex="activeIndex = $event"
      这也是双向数据绑定
       -->
    <!-- 属性修饰符 .sync 是让绑定的数据进行双向数据绑定 -->
    <!-- 语法： :属性名称="数据" :@update:属性名称="数据=$event"  简写: :属性名称.sync = "数据" -->
    <article-channel
      v-model="openChannel"
      :MyChannels="MyChannels"
      :activeIndex.sync="activeIndex"
    >
    </article-channel>
  </div>
</template>
<script>
// import { getAllChannels } from '@/api/channel'
// import GeekIcon from '@/components/geek-icon'
import { getMyChannels } from '@/api/channel'
import ArticleList from './components/article-list'
import ArticleChannel from './components/article-channel'
export default {
  name: 'HomePage',
  components: {
    ArticleList,
    ArticleChannel
  },
  data () {
    return {
      MyChannels: [],
      openChannel: false, // 控制频道组件的显示和隐藏
      activeIndex: 0
    }
  },

  // components: {
  //   GeekIcon
  // }
  // async created () {
  //   const [, res] = await getAllChannels()
  //   console.log(res.data)
  // }
  // 需求：登录完成后，更新我的频道，退出登录后，更新我的频道
  // 1.重新调用getMyChannels（）获取频道，修改this.MyChannels即可
  // 2.token变化的时候，更新我的频道
  watch: {
    '$store.state.user.token': async function () {
      const channels = await getMyChannels()
      this.MyChannels = channels
      // 登录后让它默认显示第一页  重置激活的频道为推荐（推荐不能删除）
      this.activeIndex = 0
    }
  },
  async created () {
    // 获取我的频道
    const channels = await getMyChannels()
    this.MyChannels = channels
  }
}
</script>
<style scoped lang="less">
::v-deep .van-tabs {
  height: 100%;
  display: flex;
  flex-direction: column;
  .van-tabs__line {
    background: @geek-color;
    height: 2px;
    width: 32px;
  }
  .van-tab {
    color: #9ea1ae;
  }
  .van-tab--active {
    font-size: 18px;
    color: #333;
  }
  .van-tabs__wrap {
    padding-right: 86px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  }
  .van-tabs__content {
    flex: 1;
    overflow: hidden;
  }
  .van-tab__pane {
    height: 100%;
  }
}
.home-page {
  .btn-wrapper {
    position: absolute;
    right: 0;
    top: 0;
    width: 86px;
    height: 44px;
    background: #fff;
    display: flex;
    align-items: center;
    .geek-icon {
      flex: 1;
      text-align: center;
      font-size: 18px;
    }
    &::before {
      content: "";
      width: 20px;
      height: 44px;
      position: absolute;
      left: -20px;
      top: 0;
      background: linear-gradient(to right, rgba(255, 255, 255, 0), #fff);
    }
  }
}
</style>
